package com.bosssoft.quickstart.demo.exception;

import com.bosssoft.quickstart.demo.pojo.vo.Resual;
import com.bosssoft.quickstart.demo.utils.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;
/**
 * 全局异常处理器
 **/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(ConstraintViolationException.class)
    public Resual handleConstraintException(ConstraintViolationException ex){
        log.error("触发校验异常",ex);

        return  ResultUtils.failed(BizErrorCode.PARAM_VALID_FIELD_REQUIRE);
    }

    @ExceptionHandler(NullPointerException.class)
    public Resual handleConstraintException(NullPointerException ex){
        log.error("触发异常空指针异常",ex);
        return ResultUtils.failedWithMsg("0000","空指针异常");
    }


    @ExceptionHandler(BusinessException.class)
    public Resual handleBusinessException(BusinessException ex){
        log.error("触发业务异常",ex);
        return ResultUtils.failedWithMsg(ex.getCode(),ex.getMessage());
    }


    /**
     * 处理其他所有的异常
     * @param ex 异常  demo重点 算术异常，SQL异常都归集这里处理
     * @return  公共报文模拟
     */
    @ExceptionHandler(Exception.class)
    public Resual handleConstraintException(Exception ex){
        log.error("其他类型异常",ex);
        return ResultUtils.failedWithMsg("88888","其他类型异常:"+ex.getMessage());
    }
}
